package com.aysafety.iotdataplatformclient.winds.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.WindsModel;
import com.aysafety.iotdataplatformclient.modelConfigurer.WindsModelConfiger;
import com.aysafety.iotdataplatformclient.winds.dao.WindsDao;
import com.aysafety.iotdataplatformclient.winds.services.WindsService;

@Service
public class WindsServiceImpl implements WindsService {

    @Autowired
    WindsModelConfiger windsModelConfiger;

    @Autowired
    WindsDao windsDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private WindsModel transformData(Map<String, Object> temp, Map<String, String> structure) {
        WindsModel windsModel = new WindsModel();
        for (String key : structure.keySet()) {
            String columnKey = structure.get(key);
            if ("EntId".equals(key)) {
                windsModel.setEntId(windsModelConfiger.getEntId());
            }
            if ("deviceid".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setDeviceId((String) temp.get(columnKey));
                }
            }
            if ("unit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setUnit((String) temp.get(columnKey));
                }
            }
            if ("monitoringvalue".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setMonitoringValue(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("poklad".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setPoklad(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("datetime".equals(key)) {
                String[] columnTemp = columnKey.split("\\+");
                String dateTimeStr = "";
                for (String value : columnTemp) {
                    if (temp.containsKey(value)) {
                        dateTimeStr += temp.get(value) + " ";
                    }
                }
                dateTimeStr = dateTimeStr.trim();
                windsModel.setDatetime(DateUtils.fomatDateTime(dateTimeStr));
            }
            if ("workingface".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setWorkingFace((String) temp.get(columnKey));
                }
            }
            if ("smallestrange".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setSmallestRange(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("maximumrange".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setMaximumRange(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("stationname".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setStationName((String) temp.get(columnKey));
                }
            }
            if ("status".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    int status = Integer.parseInt(temp.get(columnKey).toString());
                    if (status <= 0) {
                        windsModel.setStatus(false);
                    } else {
                        windsModel.setStatus(true);
                    }
                }
            }
            if ("powercutarea".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setPowerCutArea((String) temp.get(columnKey));
                }
            }
            if ("powerreconnectionlimit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setPowerReconnectionLimit(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("powercutlimit".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    windsModel.setPowerCutLimit(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
        }
        return windsModel;
    }



    public int readHistroyDataCount(Map<String, Object> conditions) {
        return windsDao.readHistroyDataCount(conditions);
    }

    public List<WindsModel> readHistroyData(Map<String, Object> conditions, int start, int prePage) {
        List<WindsModel> readHistroyDataModel = new ArrayList<WindsModel>();
        Map<String, String> structure = windsModelConfiger.getDatakey();
        List<Map<String, Object>> readHistroyDatas = windsDao.readHistroyData(conditions, start, prePage);
        for (Map<String, Object> readHistroyData : readHistroyDatas) {
            WindsModel windsModel = this.transformData(readHistroyData, structure);
            readHistroyDataModel.add(windsModel);
        }
        return readHistroyDataModel;
    }
}
